﻿function getDomainFromUrl(url) {
    var host = "null";
    if (typeof url == "undefined" || null == url)
        url = window.location.href;
    var regex = /.*\:\/\/([^\/]*).*/;
    var match = url.match(regex);
    if (typeof match != "undefined" && null != match)
        host = match[1];
    return host;
}

chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
    if (getDomainFromUrl(tab.url).toLowerCase() == "v.yinyuetai.com") {
        chrome.pageAction.show(tabId);
    }
});

chrome.pageAction.onClicked.addListener(function (tab) {
    chrome.tabs.sendMessage(tab.id, {greeting: "download"}, function (response) {
        if (response) {
            chrome.downloads.download(response);
            console.log(response)
            alert("下载成功");
        } else {
            alert("下载失败");
        }
    });
})
